class NumMatrix
{
public:
    NumMatrix(vector<vector<int>> &matrix)
    {
        areaPrefixSum = matrix;
        int m = matrix.size();
        int n = matrix[0].size();
        vector<int> rowPrefixSum(n);
        for (int i = 1; i < n; ++i)
        {
            areaPrefixSum[0][i] += areaPrefixSum[0][i - 1];
        }
        for (int row = 1; row < m; ++row)
        {
            rowPrefixSum[0] = matrix[row][0];
            areaPrefixSum[row][0] += areaPrefixSum[row - 1][0];
            for (int col = 1; col < n; ++col)
            {
                rowPrefixSum[col] = matrix[row][col] + rowPrefixSum[col - 1];
                areaPrefixSum[row][col] += areaPrefixSum[row - 1][col] + rowPrefixSum[col - 1];
            }
        }
    }

    int sumRegion(int row1, int col1, int row2, int col2)
    {
        int result = areaPrefixSum[row2][col2];
        if (row1 > 0)
        {
            result -= areaPrefixSum[row1 - 1][col2];
        }
        if (col1 > 0)
        {
            result -= areaPrefixSum[row2][col1 - 1];
        }
        if (row1 > 0 && col1 > 0)
        {
            result += areaPrefixSum[row1 - 1][col1 - 1];
        }
        return result;
    }

private:
    vector<vector<int>> areaPrefixSum;
};

/**
 * Your NumMatrix object will be instantiated and called as such:
 * NumMatrix* obj = new NumMatrix(matrix);
 * int param_1 = obj->sumRegion(row1,col1,row2,col2);
 */